﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

using lctdevice;

namespace FlyShoot
{
    public class Controller
    {
        public static event  EventHandler<bool> ConnectChange;
        public static int Initial(ref int slaveCount)
        {
            int ret = 0;
            int slavenum = 0;
            //Console.WriteLine("InitEcat");
            //Console.WriteLine("Press any key to continue");
            ret = MiniEcatLib.Mb_InitEcat(ref slavenum, 0);
            if (slavenum <= 0)
            {
                //throw new Exception("连接失败，请检查网口是否正确，模块power指示灯是否正常");
                return -1;
            }
            for (int i = 1; i <= slavenum; i++)
            {
                ret = MiniEcatLib.Mb_E4O4TrigOut_SetOutMode(i, 1);
                IsOnline(i);
            }
            slaveCount = slavenum;
            return 0;
        }

        public static int SetEncoderPosition(int slaveID, int channelID, int pos)
        {
            //if (IsOnline(slaveID) == 0) return 1;
            int ret = 0;
            ret |= MiniEcatLib.Mb_E4O4Encoder_Initial(slaveID, channelID, 4, 0, 1);
            //if (ret != 0) return ret;
            ret |= MiniEcatLib.Mb_E4O4Encoder_SetCurrentData(slaveID, channelID, pos);

            return ret;
        }

        public static int GetEncoderPosition(int slaveID, int channelID, ref int pos)
        {
             //if (IsOnline(slaveID) == 0) return 1;
            int ret = 0;
            ret = MiniEcatLib.Mb_E4O4Encoder_GetEncoderData(slaveID, channelID, ref pos);

            return ret;
        }

        public static int SetTrigParam(int slaveID, int channelID)
        {
            //if (IsOnline(slaveID) == 0) return 1;
            int ret = 0;

            ret |= MiniEcatLib.Mb_E4O4TrigOut_SetTrigMode(slaveID, channelID, 0);
            ret |= MiniEcatLib.Mb_E4O4TrigOut_SetPulseWidth(slaveID, channelID, 800000);
            ret |= MiniEcatLib.Mb_E4O4TrigOut_ResetCounter(slaveID, channelID);
                
            ret |= MiniEcatLib.Mb_E4O4TrigOut_BandingCompare(slaveID, channelID, 0, (uint)(1 << channelID), 0);
            ret |= MiniEcatLib.Mb_E4O4PreCmp_ResetTrigData(slaveID, channelID);
            ret |= MiniEcatLib.Mb_E4O4PreCmp_BindingEncoder(slaveID, channelID, channelID);
            //ret |= MiniEcatLib.Mb_E4O4PreCmp_SetTrigDir(slaveID, channelID, 0);

            return ret;
        }

        public static int SetTrigData(int slaveID, int channelID, int[] posArray)
        {
            //if (IsOnline(slaveID) == 0) return 1;
            int ret = 0;
            if (posArray == null)
            {
                MessageBox.Show("触发数据异常");
                return -1;
            }
            if (posArray.Length < 1)
            {
                MessageBox.Show("触发数据长度异常");
                return -1;
            }
            ret = MiniEcatLib.Mb_E4O4PreCmp_SetTrigData(slaveID, channelID, ref posArray[0], posArray.Length);

            return ret;
        }

        /// <summary>
        /// 设置方向
        /// </summary>
        /// <param name="slaveID">站</param>
        /// <param name="channelID">通道</param>
        /// <param name="dir">1正,2负,3双向</param>
        /// <returns></returns>
        public static int SetTrigDir(int slaveID, int channelID, int dir)
        {
            int ret = 0;
            ret |= MiniEcatLib.Mb_E4O4PreCmp_SetTrigDir(slaveID, channelID, dir);
            return ret;
        }

        public static int GetTrigCount(int slaveID, int channelID)
        {
            //if (IsOnline(slaveID) == 0) return 1;


            int ret = 0;
            int count = -1;
            ret = MiniEcatLib.Mb_E4O4TrigOut_GetCounter(slaveID, channelID, ref count);
            return count;

        }
        public static int ResetTrigCount(int slaveID, int channelID)
        {
            //if  

            int ret = 0;
            int count = -1;
            ret = MiniEcatLib.Mb_E4O4TrigOut_ResetCounter(slaveID, channelID);
            return count;

        }

        public static int Start(int slaveID, int channelID)
        {
            //if (IsOnline(slaveID) == 0) return 1;
            int ret = 0;
            ret = MiniEcatLib.Mb_E4O4PreCmp_SetEnable(slaveID, channelID, 0);
            ret = MiniEcatLib.Mb_E4O4PreCmp_SetEnable(slaveID, channelID, 1);

            return ret;
        }

        public static int Stop(int slaveID, int channelID)
        {
            //if (IsOnline(slaveID) == 0) return 1;
            int ret = 0;
            ret = MiniEcatLib.Mb_E4O4PreCmp_SetEnable(slaveID, channelID, 0);

            return ret;
        }


        public static int ManualOut(int slaveID, int channelID)
        {
             //if (IsOnline(slaveID) == 0) return 1;
            int ret = 0;
            ret = MiniEcatLib.Mb_E4O4TrigOut_SetManualOutput(slaveID, channelID);

            return ret;
        }

        private bool _Offline;

        public static int IsOnline(int slaveID)
        {
            var status = 0;
           
            MiniEcatLib.Mb_GetConnectStatus(slaveID, ref status);
            Controller.ConnectChange?.Invoke(slaveID, status == 1);
            return status;
        }
    }

    public class ChangedEventArgs : EventArgs
    {
        /// <summary>
        /// 设备
        /// </summary>
        public int SlaveID { get; internal set; } = -1;
        public  bool IsOnline { get; internal set; }


    }
} 
